package com.server.utils;

import java.text.DecimalFormat;

/**
 * 功能：存储固定的矩阵
 */
public class FixedParam {
    //阿尔法因子
    private final static double a = 0.163;
    //实际距离矩阵
    private final static double[][] distance = {{0, 100, 0, 400},
                                            {100, 0, 200, 300},
                                            {0, 200, 0, 0},
                                            {400, 300, 0, 0}};
    //beita因子
    private final static double b = 0.2973;
    //车辆道路宽度
    private final static double[][] width = {{0, 20, 0, 40},
                                        {20, 0, 60, 40},
                                        {0, 60, 0, 0},
                                        {40, 40, 0, 0}};
    //γ因子
    private final static double r = 0.541;
    //红绿灯个数
    private final static double[][] light = {{0, 1, 0, 3},
                                        {1, 0, 1, 3},
                                        {0, 1, 0, 0},
                                        {3, 3, 0, 0}};
    //朗布达因子
    public final static double λ = 0.5;
    //综合矩阵，由以上三个矩阵乘以各自系数再加上动态矩阵得到
    private static double[][] synthesis;
    //概率矩阵
    private static double[][] probability;
    //保留三位小数
    private final static DecimalFormat decimalFormat = new DecimalFormat("#.###");
    //初始化
    static {
        //初始化综合矩阵
        initSynthesis();
    }
    /**
     * 功能：初始化综合矩阵
     */
    private static void initSynthesis(){
        add(multiply(distance,a),multiply(width,b),multiply(light,r));
    }

    /**
     * 功能：使矩阵乘以一个数
     */
    public static double[][] multiply(double[][] m,double param){
        for (int i=0;i<m.length;i++){
            for (int j=0;j<m[i].length;j++){
                m[i][j]=m[i][j]*param;
            }
        }
        return m;
    }
    public static double[] multiply(double[] m,double param){
        for (int i=0;i<m.length;i++){
            m[i]=m[i]*param;
        }
        return m;
    }
    /**
     * 功能：使矩阵相加
     */
    public static void add(double[][] m,double[][] k,double[][] l){
        synthesis=new double[m.length][m.length];
        for (int i=0;i<synthesis.length;i++){
            for (int j=0;j<synthesis[i].length;j++){
                synthesis[i][j]=Double.parseDouble(decimalFormat.format(m[i][j]+k[i][j]+l[i][j]));
            }
        }
    }
    /**
     * 功能：使矩阵相加
     */
    public static double[] add(double[] m,double[] k){
        double[] newBytes=new double[m.length];
        for (int i=0;i<newBytes.length;i++){
            newBytes[i]=m[i]+k[i];
        }
        return newBytes;
    }
    /**
     * 功能：初始化概率矩阵
     */
    public static void initProbability(){
        probability=new double[synthesis.length][synthesis.length];
        double[] sum=new double[4];
        //得到每一行的和
        for (int i=0;i< probability.length;i++){
            for (int j=0;j< probability.length;j++){
                sum[i]+=synthesis[i][j];
            }
        }
        //求出每一行中各列的概率值
        for (int i=0;i< probability.length;i++){
            for (int j=0;j< probability.length;j++){
                probability[i][j]=Double.parseDouble(decimalFormat.format(synthesis[i][j]/sum[i]));
            }
        }
    }
    public static double[] initProbability(double[] syn){
        double[] newProbability=new double[syn.length];
        double sum=0;
        for (int i=0;i< newProbability.length;i++){
            sum+=syn[i];
        }
        //求出每一行中各列的概率值
        for (int i=0;i< newProbability.length;i++){
            newProbability[i]=syn[i]/sum;
        }
        return newProbability;
    }
    /**
     * 功能：获取概率矩阵
     * @return 概率矩阵
     */
    public static double[][] getProbability(){
        return probability;
    }
    /**
     * 功能：获取距离矩阵
     */
    public static double[][] getDistance(){
        return distance;
    }
    /**
     * 功能：获取综合矩阵
     */
    public static double[][] getSynthesis(){
        return synthesis;
    }
}
